package com.yls.except.util;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;


public class JDBCEncrypt extends PropertyPlaceholderConfigurer {
	private static final String key = "0001020110022002";

	@SuppressWarnings("static-access")
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactory, Properties props)
			throws BeansException {
		try {
			Des des = new Des();
			String username = props.getProperty("username");
			if (username != null) {
				props.setProperty("username",
						des.Decrypt(username, des.hex2byte(key)));
			}

			String password = props.getProperty("password");
			if (password != null) {
				props.setProperty("password",
						des.Decrypt(password, des.hex2byte(key)));
			}

			String url = props.getProperty("url");
			if (url != null) {
				props.setProperty("url",
						des.Decrypt(url, des.hex2byte(key)));
			}

			String driverClassName = props.getProperty("driver");
			if (driverClassName != null) {
				props.setProperty("driver",
						des.Decrypt(driverClassName, des.hex2byte(key)));
			}

			super.processProperties(beanFactory, props);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BeanInitializationException(e.getMessage());
		}
	}
}
